package com.cjd.common.fragment

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.cjd.common.R
import com.cjd.common.model.BaseModel
import kotlinx.android.synthetic.main.base_mvvm_container.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors


/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/9/25
 * description
 */
abstract class BaseMvvmFragment<M : BaseModel<*>>(
    val colorPrimary: Int = 0,
    val colorPrimaryDark: Int = 0,
    val colorAccent: Int = 0,
    val showToolbar: Boolean = false
) : BaseFragment() {

    protected var mModel: M? = null

    private val executorService: ExecutorService by lazy {
        Executors.newSingleThreadExecutor()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = super.onCreateView(inflater, container, savedInstanceState)
        if (showToolbar) {
            return includeContainer(view, inflater, container, savedInstanceState)
        }
        return view
    }

    private fun includeContainer(
        originView: View?,
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val containerView = inflater.inflate(R.layout.base_mvvm_container, container, false)
        val fltPrimaryContainer =
            containerView.findViewById<ViewGroup>(R.id.base_flt_primary_container)
        if (colorPrimary != 0) {
            val toolbar = containerView.findViewById<Toolbar>(R.id.base_toolbar)
            toolbar.setBackgroundColor(colorPrimary)
        }
        fltPrimaryContainer.addView(originView)
        return containerView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        mModel = createModel()
        mModel?.onViewCreated()
        super.onViewCreated(view, savedInstanceState)
        if (showToolbar) {
            setToolbar()
        }
    }

    private fun setToolbar() {
        setHasOptionsMenu(true)
        if (activity is AppCompatActivity) {
            val fActivity = activity as AppCompatActivity
            fActivity.setSupportActionBar(base_toolbar)
            if (displayShowHomeEnabled()) {
                fActivity.supportActionBar?.setDisplayHomeAsUpEnabled(true)
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (displayShowHomeEnabled() && item.itemId == android.R.id.home) {
            activity?.finish()
        }
        return super.onOptionsItemSelected(item)

    }

    override fun onResume() {
        super.onResume()
        mModel?.onViewResume()

    }

    override fun onPause() {
        super.onPause()
        mModel?.onViewPause()
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mModel?.onViewAttach(context)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mModel?.onViewDestroy()
        executorService?.let {
            it.shutdown()
        }
    }

    fun runOnJobThread(block: () -> Unit) {
        executorService.execute {
            block()
        }
    }

    protected fun getToolbar() = base_toolbar

    open fun displayShowHomeEnabled() = false

    protected abstract fun createModel(): M?
}